package org.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.example.domain.SysLog;
import org.example.model.Result;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.service.SysLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "系统日志接口管理")
@RestController
@RequestMapping("/sys/log")
public class SysLogController {

    @Autowired
    private SysLogService sysLogService;

    @ApiOperation("多条件分页查询系统操作日志")
    @GetMapping("/page")
    @PreAuthorize("hasAuthority('sys:log:page')")
    public Result<Page<SysLog>> loadSysLogPage(@RequestParam Long current,
                                               @RequestParam Long size,
                                               @RequestParam(required = false) Long userId,
                                               @RequestParam(required = false) String operation) {
        // 创建分页对象
        Page<SysLog> page = new Page<>(current, size);
        // 多条件分页查询系统操作日志
        sysLogService.page(page,new LambdaQueryWrapper<SysLog>()
                .eq(ObjectUtils.isNotNull(userId),  SysLog::getUserId, userId)
                .like(StringUtils.hasText(operation),  SysLog::getOperation,  operation)
                .orderByDesc( SysLog::getCreateDate)
        );
        return Result.success(  page);
    }

}
